package com.zerosky.crm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zerosky.crm.po.Document;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Information;
import com.zerosky.crm.po.Product;
import com.zerosky.crm.service.DocumentService;
import com.zerosky.crm.service.EmployeeService;
import com.zerosky.crm.service.InformationService;
import com.zerosky.crm.service.ProductService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.MyFileUploadUtils;
import com.zerosky.crm.util.PageInfo;

/**
 * 信息相关业务的Controller
 * 
 * @author 钟璟
 */
@Controller
public class InformationController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(InformationController.class); // 日志

	@Resource(name = "informationService")
	private InformationService informationService;

	@Resource(name = "employeeService")
	private EmployeeService employeeService;

	@Resource(name = "documentService")
	private DocumentService documentService;

	@Resource(name = "productService")
	private ProductService productService;
	/**
	 * 去信息管理
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toInformationJsp.do")
	public ModelAndView toInformationJsp(HttpSession session, HttpServletRequest request) {
		return new ModelAndView("WEB-INF/jsp/system/editInformation");
	}

	/**
	 * 去信息列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toListInformation.do")
	public String toListInformation(HttpSession session, HttpServletRequest request) {
		return "WEB-INF/jsp/product/informationList";
	}

	@RequestMapping("/queryInformationList.do")
	@ResponseBody
	public Map<String, Object> queryInformationList(HttpSession session, HttpServletRequest request, Integer limit,
			Integer page, String InformationName) throws Exception {
		PageInfo<Information> pageInfo = new PageInfo<Information>();
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> conditionMap = new HashMap<String, Object>();
		// 获取登录用户
		Employee user = (Employee) session.getAttribute("loginUser");
		try {
			conditionMap.put("InformationName", InformationName);
			if(!checkPower(session, 103)){
				conditionMap.put("employeeId", user.getEmployeeId());
			}
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			pageInfo = informationService.getPageInfoList(conditionMap, pageInfo);

			json.put("data", pageInfo.getList());
			json.put("total", pageInfo.getPageCount());
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping(value = "/saveInformation.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveOrUpdateInformation(Information Information, HttpServletRequest request,
			HttpServletResponse response, HttpSession session, MultipartFile fileGraphicsText) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String StrId = UUID.randomUUID().toString();
			List<Document> list =new ArrayList<Document>();
			if (Information.getHotelImgFile() != null) {
				String path = "upload/";
				for (MultipartFile MultipartFile : Information.getHotelImgFile()) {
					MyFileUploadUtils.saveFile(request, MultipartFile, path);
					if (request.getAttribute("picture") != null) {
						Document document = new Document();
						document.setRecordId(StrId);
						document.setType("2");
						document.setUrl(path + request.getAttribute("picture"));
						document.setNewName(request.getAttribute("picture").toString());
						document.setName(MultipartFile.getOriginalFilename());
						list.add(document);
					}
				}
			}
			// 获取登录用户
			Employee user = (Employee) session.getAttribute("loginUser");
			Information.setCreateDate(new Date());
			Information.setInfoId(StrId);
			Information.setCreateEmpId(user.getEmployeeId());
			Information.setInfoStatus("0");
			Integer code = informationService.saveOrUpdate(Information, "新增");

			if (code > 0) {
				if(list.size()>0){
					documentService.addDocumentList(list);
				}
				map.put("code", 0);
			} else {
				map.put("code", 1);
			}
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
			map.put("code", -1);
		}
		return map;
	}

	@RequestMapping(value = "deleteInformation.do")
	@ResponseBody
	public Map<String, Object> deleteInformation(HttpSession session, HttpServletRequest request,
			HttpServletResponse response, String InformationId) throws Exception {
		Map<String, Object> map = new HashMap<>();
		try {
			Integer code = informationService.deleteInformation(InformationId);
			if (code > 0) {
				map.put("code", 0);
			} else {
				map.put("code", 1);
			}
		} catch (Exception e) {
			map.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return map;
	}

	/**
	 * 
	 * 修改前根据id查询出来修改的信息
	 * 
	 * @param request
	 * @param InformationId
	 * @return
	 */
	@RequestMapping("toModifyInformation.do")
	@ResponseBody
	public Map<String, Object> toModifyInformation(HttpSession session, HttpServletRequest request,
			String InformationId) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Information Information = informationService.getInformationById(InformationId);
			if (Information != null) {
				Employee emp = employeeService.getEmployeeById(Information.getCreateEmpId());
				// 数据
				Information.setCreatePeople(emp.getName());
				// 获取所有产品信息
				List<Product> productList = productService.getProductList(json);
				json.put("productList", productList);
				json.put("code", 0);
				json.put("data", Information);
			} else {
				json.put("code", 1);
			}

		} catch (Exception e) {
			json.put("code", "-1");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	@RequestMapping("modifyInformation.do")
	@ResponseBody
	public Map<String, Object> modifyInformation(HttpSession session, HttpServletRequest request,
			Information Information) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			List<Document> list =new ArrayList<Document>();
			if (Information.getHotelImgFile() != null) {
				String path = "upload/";
				for (MultipartFile MultipartFile : Information.getHotelImgFile()) {
					MyFileUploadUtils.saveFile(request, MultipartFile, path);
					if (request.getAttribute("picture") != null) {
						Document document = new Document();
						document.setRecordId(Information.getInfoId());
						document.setType("2");
						document.setUrl(path + request.getAttribute("picture"));
						document.setNewName(request.getAttribute("picture").toString());
						document.setName(MultipartFile.getOriginalFilename());
						list.add(document);
					}
				}
			}
			Integer count = informationService.saveOrUpdate(Information, "修改");
			
			if (count > 0) {
				if(list.size()>0){
					documentService.deleteDocumentById(Information.getInfoId());
					documentService.addDocumentList(list);
				}
				json.put("code", 0);
			} else {
				json.put("code", 1);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
}
